//
//  TOLiveDetailViewController.m
//  SeaGullFM
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 tianou. All rights reserved.
//

#import "TOLiveDetailViewController.h"
#import "TOLiveTextTableViewCell.h"

@interface TOLiveDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)TOBaseTableView *myTableView;
@property(nonatomic,retain)NSMutableArray *myArray;

@end

@implementation TOLiveDetailViewController

- (void)dealloc
{
    [_myArray release];
    [_myTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self getData];
    [self createTableView];
    
    //返回按钮
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0 , 20, 30);
    backButton.backgroundColor = CLEAR;
    [backButton setImage:[UIImage imageNamed:@"btn_back_n@2x.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"btn_back_h@2x.png"] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
   
}

#pragma mark - 返回按钮方法
-(void)backButtonAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
    //    [[NSNotificationCenter defaultCenter] postNotificationName:@"createBigButton" object:[TOPlayButton shareButton]];
    
}

#pragma mark - 请求数据
-(void)getData
{
    NSString *urlStr = @"http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=1&radioType=1&device=iphone&pageSize=15";
    [TOAppTools getDataWithURL:urlStr block:^(id result) {
        
        self.myArray = [NSMutableArray array];
        
        NSMutableArray *array = [result objectForKey:@"result"];
        for (NSMutableDictionary *dic in array) {
            
            TOLiveProgramModel *model = [TOLiveProgramModel modelWithDictionary:dic];
            [self.myArray addObject:model];
        }
    
        [self.myTableView reloadData];

    } error:^(NSError *error) {
        
        NSLog(@"请求数据失败  error" );
    }];
}

#pragma mark - 创建tableView
-(void)createTableView
{
    self.myTableView = [[TOBaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.9) style:UITableViewStylePlain];
    self.myTableView.backgroundColor = COLOR_BG;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [self.myTableView registerClass:[TOLiveTextTableViewCell class] forCellReuseIdentifier:@"liveTextTableCell"];
 
    self.myTableView.bounces = NO;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

#pragma mark - tableView协议方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.myArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TOLiveTextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"liveTextTableCell"];
    
    TOLiveProgramModel *model = [self.myArray objectAtIndex:indexPath.row];
    
    cell.programeModel = model;
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT*0.16;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
